package com.xunce.xcworkservice.controller;

import com.xunce.xcworkservice.dto.LoginDto;
import com.xunce.xcworkservice.entity.Account;
import com.xunce.xcworkservice.exception.CustomException;
import com.xunce.xcworkservice.response.R;
import com.xunce.xcworkservice.service.AccountService;
import com.xunce.xcworkservice.vo.UserInfoVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author macmini
 */
@RestController
@RequiredArgsConstructor
@Validated
@Tag(name = "登录退出")
public class LoginController extends BaseController {

    private final AccountService accountService;

    @PostMapping("/login")
    @Operation(summary = "登录方法", description = "测试描述")
    public R<UserInfoVo> login(@Valid @RequestBody LoginDto loginDto) throws CustomException {

        UserInfoVo account = accountService.login(loginDto);
        return R.success(account);
    }

    @PostMapping("/logout")
    @Operation(summary = "退出登录", description = "测试描述")
    public R<String> logout(){

        accountService.logOut();
        return R.success();
    }

    @GetMapping("/userInfo")
    @Operation(summary = "用户信息", description = "测试描述")
    public R<Object> userInfo(){
        Subject subject = SecurityUtils.getSubject();
        PrincipalCollection principals = subject.getPrincipals();
        UserInfoVo userInfoVo = (UserInfoVo) principals.getPrimaryPrincipal();
        List<UserInfoVo> userInfoVoList = new ArrayList<>();
        userInfoVoList.add(userInfoVo);
        accountService.batchSetAccountFillInfo(userInfoVoList);
        return R.success(userInfoVo);
    }
}
